
class TextSplitter:
    def __init__(self, separators=['。'],chunk_size=512,) -> None:
        self.separators = separators
        self.chunk_size = chunk_size

    def split_text(self, text:str) -> list:
        previous_sep = 0
        cur_text_len = 0
        cur_texts = []
        texts = []
        for cur_i in range(len(text)):
            if text[cur_i] in self.separators:
                cur_text_len += cur_i + 1 - previous_sep
                if cur_text_len > self.chunk_size:
                    texts.append(''.join(cur_texts))
                    cur_texts.clear()
                    cur_text_len = 0

                cur_text_len += cur_i + 1 - previous_sep
                cur_texts.append(text[previous_sep:cur_i + 1])
                previous_sep = cur_i + 1
        if len(cur_texts) > 0: texts.append(''.join(cur_texts))
        return texts
